package com.check.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.check.dao.AgencyDao;
import com.check.dao.PersonDao;
import com.check.dao.PersonExpressDao;
import com.check.pojo.ExpressDetail;
import com.check.pojo.ExpressDetailForm;
import com.check.pojo.ExpressOther;
import com.check.pojo.Opr;
import com.check.pojo.Org;
import com.check.service.PersonExpressService;
import com.check.util.StringUtil;

@Service("personExpressService")
public class PersonExpressServiceImpl implements PersonExpressService{
	
	@Autowired
	@Qualifier("personExpressDao")
	private PersonExpressDao personExpressDao;
	
	@Autowired
	@Qualifier("agencyDao")
	private AgencyDao agencyDao;
	
	@Autowired
	@Qualifier("personDao")
	private PersonDao personDao;

	public String getPersonExpressTree(String id) {
		Map map=new HashMap();
		map.put("upid", id);
		List<Org> orgList=agencyDao.findAllOrgByUpId(map);
		List<Opr> oprList=personDao.findAllOprByUpid(id);	
		StringBuilder sb=new StringBuilder("[");
		for (int i = 0; i < oprList.size(); i++) {
			Opr opr = oprList.get(i);
			if(StringUtil.isBlank(opr.getDeptId()) && StringUtil.isBlank(opr.getRoomId()) && StringUtil.isBlank(opr.getOrgId())){
				continue;
			}
			sb.append("{");
			sb.append("id:'"+opr.getOprId()+"',");
			sb.append("pId:'"+opr.getOrgId()+"',");
			sb.append("name:'"+opr.getOprName()+"',");
			sb.append("open:false,");
			sb.append("type:'1'");
			if(i==oprList.size()-1){
				sb.append("}");
			}else{
				sb.append("},");
			}
	}
		if(orgList.size()>0 && oprList.size()>0){
			sb.append(",");
		}
		
		for (int i = 0; i < orgList.size(); i++) {
				Org org=orgList.get(i);
				sb.append("{");
				sb.append("id:'"+org.getO_id()+"',");
				sb.append("pId:'"+org.getO_upid()+"',");
				sb.append("name:'"+org.getO_name()+"',");
				if(org.getO_orgcount()>0){
					sb.append("isParent:true,");
				}else if(org.getO_orgcount()==0 && org.getO_oprcount()>0){
					sb.append("isParent:true,");
				}else{
					sb.append("isParent:false,");
				}
				sb.append("type:'0',");
				sb.append("open:false");
				if(i==orgList.size()-1){
					sb.append("}");
				}else{
					sb.append("},");
				}
		}
	
	
		sb.append("]");
		
		return sb.toString();
	}

	public List<ExpressDetail> findPersonExpress(String id,String type) {
		Map map=new HashMap();
		map.put("checkId", id);
		map.put("expType", type);
		return personExpressDao.findPersonExpress(map);
	}

	public boolean updatePersonExpress(ExpressDetailForm expressDetailForm,List<ExpressDetail> expressDetailList) {
		boolean flag=false;
		try {
			Map map=new HashMap();
			map.put("checkId", expressDetailForm.getCheckId());
			map.put("expType", expressDetailForm.getExpType());
			personExpressDao.deletePersonExpress(map);
			for (ExpressDetail expressDetail : expressDetailList) {
				personExpressDao.insertPersonExpress(expressDetail);
			}
			
			flag=true;
		} catch (Exception e) {
			e.printStackTrace();
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			flag=false;
		}
		return flag;
	}
}
